package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.*;
import com.cskaoyan.shopping.constants.GlobalConstants;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemDesc;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.ItemDescMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * @author Zhang Tao
 * @version 1.0.0
 * @description
 * @date since 2022-07-29 17:28
 */
@Slf4j
@Service
public class IProductDetailServiceImpl implements IProductService {
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ItemDescMapper descMapper;
    @Autowired
    ProductConverter productConverter;
    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ContentConverter contentConverter;
/*
 * @description:查看商品详情
 * @author: zhang tao
 * @date: 2022-07-29 22:46
 * @param:
 * @return:
 **/
    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse response = new ProductDetailResponse();
        ProductDetailDto detailDto;
        try {
            request.requestCheck();
            //根据商品id查商品信息
            Item item = itemMapper.selectByPrimaryKey(request.getId());
            //根据商品id查商品详情
            ItemDesc itemDesc = descMapper.selectByPrimaryKey(request.getId());
            detailDto = new ProductDetailDto(item.getId(), item.getPrice(),
                    item.getTitle(), item.getSellPoint(), (long) item.getNum(),
                    item.getImageBig(), itemDesc.getItemDesc(),
                    Arrays.asList(item.getImages()));
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            response.setProductDetailDto(detailDto);
        } catch (Exception e) {
            log.error("IProductDetailImpl.getProductDetail occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }


        return response;
    }
    /*
     * @description:查看所有商品
     * @author: zhang tao
     * @date: 2022-07-29 22:46
     * @param:
     * @return:
     **/
    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        AllProductResponse response = new AllProductResponse();
        try {
            request.requestCheck();
            PageHelper.startPage(request.getPage(), request.getSize());
            Example example = new Example(Item.class);
            Example.Criteria criteria = example.createCriteria();
            //显示上架商品
            criteria.andEqualTo("status", 1);
            if (request.getCid() != null) {
                //添加cid条件
                criteria.andEqualTo("cid", request.getCid());
            }
            if(request.getPriceLte()!=null){
                //添加价格下限
                criteria.andLessThanOrEqualTo("price",request.getPriceLte());
            }
            if(request.getPriceGt()!=null){
                //添加价格上限
                criteria.andGreaterThan("price",request.getPriceGt());
            }
            if (request.getSort() != null) {
                if (request.getSort().equals("1")) {
                    //按价格升序排列
                    example.setOrderByClause("price");
                } else if (request.getSort().equals("-1")) {
                    //按价格降序排列
                    example.setOrderByClause("price desc");
                }
            }else{
                //按创建时间降序排列
                example.setOrderByClause("created desc");
            }

            List<Item> items = itemMapper.selectByExample(example);
            PageInfo<Item> pageInfo = new PageInfo<>(items);
            List<ProductDto> productDtos = productConverter.items2Dto(items);
            response.setProductDtoList(productDtos);
            response.setTotal(pageInfo.getTotal());
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            log.error("IProductDetailImpl.getAllProduct occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);

        }

        return response;
    }
    /*
     * @description:查看推荐商品
     * @author: zhang tao
     * @date: 2022-07-29 22:46
     * @param:
     * @return:
     **/
    @Override
    public RecommendResponse getRecommendGoods() {
        RecommendResponse response = new RecommendResponse();
        try {
            // Example example = new Example(Panel.class);
            // example.setOrderByClause("sort_order");
            // example.createCriteria().andEqualTo("status", 1)
            // .andEqualTo("remark","热门推荐");
            // List<Panel> panels = panelMapper.selectByExample(example);
            //推荐商品板块id
            List<Panel> panels = panelMapper.selectPanelContentById(GlobalConstants.RECOMMEND_PANEL_ID);
            Set<PanelDto> panelDtos=new HashSet<>();
            for (Panel panel : panels) {
                //板块内容
                List<PanelContentItem> panelContentItems = panelContentMapper.
                        selectPanelContentAndProductWithPanelId(panel.getId());
             //转化成板块内容dto
                List<PanelContentItemDto> panelContentItemDtos =
                        contentConverter.panelContentItem2Dto(panelContentItems);
                PanelDto panelDto = contentConverter.panel2Dto(panel);
               panelDto.setPanelContentItems(panelContentItemDtos);
               //添加
               panelDtos.add(panelDto);
            }
          response.setPanelContentItemDtos(panelDtos);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            log.error("IProductDetailImpl.getRecommendGoods occur Exception :" + e);
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }

    @Override
    public AllItemResponse getAllItems() {
        return null;
    }
}
